package com.glory.brepda.activity.mfg;

import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.glory.brepda.R;
import com.glory.brepda.adapter.MaterielAdapter;
import com.glory.brepda.base.BaseActivity;
import com.glory.brepda.callback.CustomDialogEntityCallBack;
import com.glory.brepda.entity.BomEntity;
import com.glory.brepda.entity.EqpEntity;
import com.glory.brepda.entity.LotAndPackagingfileEntity;
import com.glory.brepda.entity.LotHoldEntity;
import com.glory.brepda.entity.LotInfoEntity;
import com.glory.brepda.entity.SuspendCodeBean;
import com.glory.brepda.entity.WoPartWobomline;
import com.glory.brepda.entity.response.BaseResponseBean;
import com.glory.brepda.utils.LotUtils;
import com.glory.brepda.utils.NetUtils;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.model.Response;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_DialogEntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_EntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.dialog.FreeUI_GeneralPop;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_PopUtils;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_StaticMembers;
import com.pda.platform.ui.ui_pdaplatform.view.FreeUI_ClearEditText;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;

import butterknife.BindView;
import butterknife.OnClick;

public class StripReprintActivity extends BaseActivity {

    @BindView(R.id.tvTitle)
    TextView tvTitle;
    @BindView(R.id.llMain)
    LinearLayout llMain;
    @BindView(R.id.tvBaseSubmit)
    TextView tvBaseSubmit;
    @BindView(R.id.etWoID)
    FreeUI_ClearEditText etWoID;
    @BindView(R.id.etStripIDNum)
    FreeUI_ClearEditText etStripIDNum;
    @BindView(R.id.etComments)
    FreeUI_ClearEditText etComments;
    @BindView(R.id.tvRLF)
    TextView tvRLF;
    @BindView(R.id.tvPrintCount)
    TextView tvPrintCount;
    private BigDecimal stripcount;
    private String RLFQTY,CHIPRATE;

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_strip_reprint;
    }

    @Override
    protected void initView() {

        String title = getIntent().getStringExtra(FreeApi_StaticMembers.SHOW_PLUGIN_KEY);
        tvBaseSubmit.setText("提交");
        tvTitle.setText(title);
        setMainLayout(llMain);
    }

    @Override
    protected void initData() {
        initEdit(etWoID,s ->{

            //通过片号获取相关信息
            FreeUI_EntityCallBack<BaseResponseBean<WoPartWobomline>> callBack = new FreeUI_DialogEntityCallBack<BaseResponseBean<WoPartWobomline>>
                    (new TypeToken<BaseResponseBean<WoPartWobomline>>() {
                    }.getType(), getSupportFragmentManager(), this) {

                @Override
                public void onSuccess(Response<BaseResponseBean<WoPartWobomline>> response) {
                    super.onSuccess(response);
                    if (response.body().isSuccess(getApplicationContext(), true, getSupportFragmentManager())) {
                        //赋值框架型号
                        for(int i=0;i<response.body().getResponse().getBody().getWORKORDERBOMLINELIST().size();i++){
                            if(response.body().getResponse().getBody().getWORKORDERBOMLINELIST().get(i).getMaterialType().equals("RLF")){
                                tvRLF.setText(response.body().getResponse().getBody().getWORKORDERBOMLINELIST().get(i).getMATERIALDESC());
                                break;
                            }
                        }
                        RLFQTY = response.body().getResponse().getBody().getRLFQTY();
                        CHIPRATE = response.body().getResponse().getBody().getCHIPRATE();
                    }
                }

                @Override
                public void onError(Response<BaseResponseBean<WoPartWobomline>> response) {
                    super.onError(response);
                    loadError(response.getException(), "失败");
                }
            };
            JsonObject map = new JsonObject();
            map.addProperty("WORDER", getText(etWoID));
            map.addProperty("ACTION", "WORDER_QUERY");
            NetUtils.requestPostNet("IF.WORDER_QUERY_INFOS", "Common", getApplication(), map, callBack);
        });

        initEdit(etStripIDNum,s ->{
            BigDecimal stripnum=new BigDecimal(getText(etStripIDNum));
            stripcount = stripnum.divide(new BigDecimal(RLFQTY).multiply(new BigDecimal(CHIPRATE == null? "100" : CHIPRATE)).divide(BigDecimal.valueOf(100)),0, RoundingMode.CEILING);
            tvPrintCount.setText(stripcount.toString());
            etComments.requestFocus();
        });
    }

    @OnClick({R.id.tvBaseSubmit})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tvBaseSubmit:
                if (etStripIDNum.equals("")){
                    showToast("数量不能为空",true);
                    return;
                }
                if (getText(etComments).equals("")){
                    showToast("请输入补打备注",true);
                    return;
                }
                FreeUI_EntityCallBack<BaseResponseBean<LotAndPackagingfileEntity>> callBack = new FreeUI_DialogEntityCallBack<BaseResponseBean<LotAndPackagingfileEntity>>
                        (new TypeToken<BaseResponseBean<LotAndPackagingfileEntity>>() {
                        }.getType(), getSupportFragmentManager(), this) {

                    @Override
                    public void onSuccess(Response<BaseResponseBean<LotAndPackagingfileEntity>> response) {
                        super.onSuccess(response);
                        if (response.body().isSuccess(getApplicationContext(), true, getSupportFragmentManager())) {
                            showToast("补打成功", true);
                            finish();
                        }
                    }

                    @Override
                    public void onError(Response<BaseResponseBean<LotAndPackagingfileEntity>> response) {
                        super.onError(response);
                        loadError(response.getException(), "失败");
                    }
                };
                JsonObject map = new JsonObject();
                map.addProperty("WOID", getText(etWoID));
                map.addProperty("ACTIONTYPE", "REPRINT");
                map.addProperty("COMMENTS", getText(etComments));
                map.addProperty("PRINTCOUNT", tvPrintCount.getText().toString());
                NetUtils.requestPostNet("IF.WIP_WO_STRIPID_REPRINT", "Common", getApplication(), map, callBack);

                break;

        }
    }
}
